//
//  CYGuideViewController.m
//  cyjf
//
//  Created by tauCross on 2017/8/8.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYGuideViewController.h"



@interface CYGuideViewController ()

@property(nonatomic, strong)UIScrollView *guideScrollView;
@property(nonatomic, strong)NSMutableArray *guideViews;

@end


@implementation CYGuideViewController

+ (NSNumber *)currentGuideVersion
{
    return @4;
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (void)loadUI
{
    self.tableView.hidden = YES;
    
    self.guideScrollView = [[UIScrollView alloc] init];
    self.guideScrollView.pagingEnabled = YES;
    self.guideScrollView.bounces = NO;
    self.guideScrollView.delegate = self;
    self.guideScrollView.showsVerticalScrollIndicator = NO;
    self.guideScrollView.showsHorizontalScrollIndicator = NO;
    [self.contentView addSubview:self.guideScrollView];
    
    NSArray *guideImages = @[@"guide_1",
                             @"guide_2",
                             @"guide_3",
                             @"guide_4"];
    NSArray *guidePages = @[@"guide_page_1",
                            @"guide_page_2",
                            @"guide_page_3",
                            @"guide_page_4"];
    self.guideViews = [NSMutableArray arrayWithCapacity:4];
    for(NSString *imageName in guideImages)
    {
        UIView *view = [[UIView alloc] init];
        UIImageView *guideImageView = [[UIImageView alloc] initWithImage:CYImage(imageName)];
        guideImageView.contentMode = UIViewContentModeScaleAspectFill;
        guideImageView.layer.masksToBounds = YES;
        [view addSubview:guideImageView];
        [self.guideScrollView addSubview:view];
        [self.guideViews addObject:view];
        
        UIImageView *guidePage = [[UIImageView alloc] initWithImage:CYImage(guidePages[[guideImages indexOfObject:imageName]])];
        [view addSubview:guidePage];
        
        @weakify(view);
        @weakify(guideImageView);
        @weakify(guidePage);
        [TCRACObserve(view, frame) subscribeNext:^(id x) {
            @strongify(view);
            @strongify(guideImageView);
            @strongify(guidePage);
            guideImageView.frame = view.bounds;
            guidePage.centerX = view.width / 2;
            guidePage.top = view.height - 46;
        }];
        
        if(imageName == guideImages.lastObject)
        {
            UIButton *enterButton = [UIButton cy_mainButtonWithTitle:@"立即体验"];
            [enterButton addTarget:self action:@selector(onEnterButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
            enterButton.layer.cornerRadius = 8;
            enterButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
#ifdef CYJF_HUIKUI
            enterButton.height = 45;
            enterButton.width = 290;
#else
            enterButton.height = 50;
            enterButton.width = 250;
#endif
            [view addSubview:enterButton];
            @weakify(enterButton);
            [TCRACObserve(view, frame) subscribeNext:^(id x) {
                @strongify(enterButton);
                @strongify(view);
                enterButton.centerX = view.width / 2;
                enterButton.bottom = view.height - 140;
            }];
        }
    }
}

- (void)layoutUI
{
    self.guideScrollView.frame = self.contentView.bounds;
    self.guideScrollView.contentSize = CGSizeMake(self.guideScrollView.width * self.guideViews.count, self.guideScrollView.height);
    for(UIView *view in self.guideViews)
    {
        view.frame = self.guideScrollView.bounds;
        view.x = [self.guideViews indexOfObject:view] * self.guideScrollView.width;
    }
}

#pragma mark - Control Events
- (void)onEnterButtonTouchUpInside
{
    if(self.complete)
    {
        self.complete();
    }
}

@end
